<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/13
 * Time: 14:44
 */

namespace app\models;
use yii\db\ActiveRecord;

class Bar_claim extends ActiveRecord
{
	// 获取商家入驻信息
	public function getBusinessList($limit, $offset, $isPass, $barType, $barName, $endTime, $startTime, $examineEnd, $examineStart){
		$post = Bar_claim::find()->joinWith('bar', 'user');
		if ($isPass != -1) {
			$post->andWhere(['is_pass'=>$isPass]);
		}

		if ($barType != -1) {
			$post->andWhere(['bar_bar.bar_type'=>$barType]);
		}

		if ($barName != -1) {
			$post->andWhere(['bar_bar.bar_name'=>$barName]);
		}

		if ($startTime != -1) {
            $post->andWhere(['between', 'bar_claim.gmt_create', $startTime, $endTime]);
        }

        if ($examineStart != -1) {
            $post->andFilterWhere(['between', 'bar_claim.gmt_modified', $examineStart, $examineEnd]);
        }

		$count = $post->count();
        $result = $post->limit($limit)->offset($offset)->asArray()->all();
        return ['total' => $count, 'rows' => $result];
	}

	// 关联商家用户表
    public function getBar()
    {
        //同样第一个参数指定关联的子表模型类名
        //第二个参数第一个是字表的 第二个是主表的
        return $this->hasOne(Bar_bar::className(), ['bar_id' => 'bar_id']);
    }

    // 关联商家用户表
    public function getUser()
    {
        //同样第一个参数指定关联的子表模型类名
        //第二个参数第一个是字表的 第二个是主表的
        return $this->hasOne(User_user::className(), ['user_id' => 'user_id']);
    }

    // 通过入驻
    public function editPass($claimId, $isPass, $barId) {
        if ($barId != -1) {      
            $pass = Bar_claim::findAll(['bar_id' => $barId, 'is_pass' => '1']);
            if ($pass) {
                return '3';
            } else {
                $data = Bar_bar::findOne(['bar_id' => $barId]);
                $data->is_claim = '1';
                $data->save();

                $post = Bar_claim::findOne(['claim_id' => $claimId]);
                $post->is_pass = $isPass;

                return $post->save();
            }
        } else {
            $post = Bar_claim::findOne(['claim_id' => $claimId]);
            $post->is_pass = $isPass;

            return $post->save();
        }
    }
}